package com.niit.drugsmanager.controller;

import com.niit.drugsmanager.entity.Drug;
import com.niit.drugsmanager.entity.User;
import com.niit.drugsmanager.service.DrugService;
import com.niit.drugsmanager.service.UserService;
import com.niit.drugsmanager.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/api/drugs")
public class DrugController {

    @Autowired
    private DrugService drugService;
    @Autowired
    private UserService userService;


    // 显示所有药品列表
    @GetMapping
    public String getAllDrugs(
            Model model,
            RedirectAttributes redirectAttributes,
            HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0011);
        boolean flag2 = user.hasPermission(0b1000);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";
        if(!user.hasRole(0b0001)) return "redirect:/api/user/ban";

        model.addAttribute("drugs", drugService.getAllDrugs());
        return "drugs/list";
    }

    // 显示药品详情
    @PostMapping("/detail")
    public String getDrugDetails(
            @RequestParam("did") Long did,
            Model model,
            HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0011);
        boolean flag2 = user.hasPermission(0b1000);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

        Drug drug = drugService.getDrugById(did);
        model.addAttribute("drug", drug);
        return "drugs/detail";
    }

    // 显示创建表单
    @PostMapping("/new")
    public String showCreateForm(
            Model model,
            HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0101);
        boolean flag2 = user.hasPermission(0b1000);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

        model.addAttribute("drug", new Drug());
        return "drugs/form";
    }

    // 显示编辑表单
    @PostMapping("/edit")
    public String showEditForm(
            @RequestParam("did") Long did,
            Model model,
            HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0101);
        boolean flag2 = user.hasPermission(0b1000);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

        Drug drug = drugService.getDrugById(did);
        model.addAttribute("drug", drug);
        return "drugs/form";
    }

    // 处理创建/更新提交
    @PostMapping("/save")
    public String saveDrug(
            @ModelAttribute("drug") Drug drug,
            RedirectAttributes redirectAttributes,
            HttpSession session) {
        try {
            //登录检查
            User user = (User) session.getAttribute("user");
            String s = SessionUtils.ifUserNull(user, userService);
            if(s != null) return s;

            //权限检查
            boolean flag1 = user.hasRole(0b0101);
            boolean flag2 = user.hasPermission(0b1000);
            if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

            if (drug.getId() == null) {
                drugService.createDrug(drug);
                redirectAttributes.addFlashAttribute("successMessage", "药品创建成功");
            } else {
                drugService.updateDrug(drug);
                redirectAttributes.addFlashAttribute("successMessage", "药品更新成功");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", "操作失败: " + e.getMessage());
        }
        return "redirect:/api/drugs";
    }

    // 处理删除请求
    @PostMapping("/delete")
    public String deleteDrug(
            @RequestParam("did") Long did,
            RedirectAttributes redirectAttributes,
            HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0101);
        boolean flag2 = user.hasPermission(0b1000);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

        try {
            drugService.deleteDrug(did);
            redirectAttributes.addFlashAttribute("successMessage", "药品删除成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", "删除失败: " + e.getMessage());
        }
        return "redirect:/api/drugs";
    }
}